const socketWrapper = require('./index');
const { redisGet } = require('../utils/redis')
const { sendNotification } = require('../mq/chatMsg');

// 等待 Socket.IO 初始化完成后再使用
function setChat() {
    try {
        const io = socketWrapper.getIO();
        io.on('connection', async (socket) => {
            // 消息类型定义：1文字 2 图片 3表情 4小提示文字, 5 视频 6文件 7 加价 8 分享商品 9刚刚浏览的商品, 10 邀请支付
            socket.on('chatMsg', async (data, callback = () => { }) => {
                console.log('chatMsg', data)
                if (!data.to) {
                    callback({
                        code: 500,
                        message: 'to not found'
                    })
                    return;
                }
                if (!data.content) {
                    callback({
                        code: 500,
                        message: 'content not found'
                    })
                    return;
                }
                if (!data.contentType) {
                    callback({
                        code: 500,
                        message: 'contentType not found'
                    })
                    return;
                }
                const toUserInfo = await redisGet(`mim-${data.to}`);
                if (!toUserInfo) {
                    callback({
                        code: 500,
                        message: '未找到该用户IM信息'
                    })
                    return;
                }
                if (toUserInfo.imKey === socket.imKey) {
                    callback({
                        code: 500,
                        message: '不能给自己发送消息'
                    })
                    return;
                }
                if (toUserInfo.online === 0) {
                    callback({
                        code: 1,
                        message: '对方不在线'
                    });
                }
                const formUserInfo = await redisGet(`mim-${socket.imKey}`);
                if (!formUserInfo) {
                    callback({
                        code: 500,
                        message: '未找到该用户IM信息'
                    });
                    return;
                }
                const msgData = {
                    from: formUserInfo.imKey,
                    fromName: formUserInfo.userName,
                    fromAvatar: formUserInfo.avatar,
                    fromType: formUserInfo.userType,
                    to: toUserInfo.imKey,
                    toName: toUserInfo.userName,
                    toAvatar: toUserInfo.avatar,
                    toType: toUserInfo.userType,
                    content: data.content,
                    contentType: data.contentType,
                    exData: data.exData || {},
                    time: new Date().getTime(),
                }
                if (data.msgKey) msgData.msgKey = data.msgKey;
                callback({
                    code: 200,
                    data: msgData
                });
                await sendNotification(msgData);
            });
        });
    } catch (error) {
        console.error('Socket.IO 未初始化:', error);
    }
}

module.exports = setChat;
